package com.yoomiito.app.ui.video.clip

import android.content.Context
import android.content.Intent
import android.os.Bundle
import cn.jzvd.Jzvd
import cn.jzvd.JzvdStd
import com.gyf.barlibrary.ImmersionBar
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.model.BaseList
import com.yoomiito.app.model.home.VideoClipInfo
import com.yoomiito.app.model.video.VideoOtherInfo
import com.yoomiito.app.utils.Const
import com.yoomiito.app.utils.L
import com.yoomiito.app.widget.ScrollLinearLayouManager
import kotlinx.android.synthetic.main.act_video_clip.*
import kotlinx.android.synthetic.main.act_video_clip.smartRefreshLayout

class VideoClipListActivity : BaseActivity<VideoClipListPresenter>() {

    companion object {
        fun start(context: Context, playPosition: Int = -1,data:ArrayList<VideoClipInfo>) {
            val intent = Intent(context, VideoClipListActivity::class.java)
            intent.putExtra(Const.Common.Position, playPosition)
            intent.putParcelableArrayListExtra(Const.Common.Data,data)
            context.startActivity(intent)
        }
    }

    private val mAdapter = VideoClipAdapter(null)
    private var mPlayPosition = -1//进入列表 播放哪个页面
    private var mCurrentPage = 1//当前页 用于请求数据
    private var mRcyCurrentPosition = -1 //用于记录rcy滚动到的当前页
    private val mSize = 10
    private var oldData:ArrayList<VideoClipInfo>?=null
    override fun getLayoutId(): Int = R.layout.act_video_clip

    override fun newP(): VideoClipListPresenter = VideoClipListPresenter(App.mAppConfig)

    override fun initImmersionBar() {
        ImmersionBar.with(this).keyboardEnable(false).init()
    }

    override fun initData(savedInstanceState: Bundle?) {
        super.initData(savedInstanceState)
        mPlayPosition = intent.getIntExtra(Const.Common.Position, -1)
        oldData  = intent.getParcelableArrayListExtra<VideoClipInfo>(Const.Common.Data)
        back.setOnClickListener { finish() }
        smartRefreshLayout.setEnableRefresh(false)
        smartRefreshLayout.setOnLoadMoreListener {
            mCurrentPage++
            requestData()
        }
        initRcy()
        mRcyCurrentPosition = mPlayPosition//记录第一次滚动时 rcy选中的位置
        if (mPlayPosition<10){
            mAdapter.setNewData(oldData)
            rcy.scrollToPosition(mPlayPosition)
            p.getVideoDetail(mAdapter.data[mPlayPosition].id)
        }else{
            showLoading()
            mCurrentPage = 2
            requestData()
        }

    }

    private fun requestData() {
        p.getVideoList(mCurrentPage,mSize)
    }

    private fun initRcy() {
        val layoutManager = ScrollLinearLayouManager(this)
        layoutManager.onPageScrollListener = {
            if (!mAdapter.autoPlay)mAdapter.autoPlay = true
            if (mRcyCurrentPosition != it) {
                mRcyCurrentPosition = it
                p.getVideoDetail(mAdapter.data[it].id)
            }
        }

        rcy.layoutManager = layoutManager
        rcy.adapter = mAdapter
//        rcy.addOnChildAttachStateChangeListener(object : RecyclerView.OnChildAttachStateChangeListener {
//            override fun onChildViewDetachedFromWindow(p0: View) {
//                L.i("SS退出：$p0")
//                val jzvd = p0.findViewById<VideoPlayer>(R.id.videoPlayer)
//                jzvd.playStateListener={state,value->
//                    L.i("SSS播放中")
//                        if (state==VideoPlayer.PLAYING){
//                            L.i("SSS播放中00")
//
//                            if (jzvd != null) {
//                                L.i("SSS播放中12")
//
//                                Jzvd.releaseAllVideos()
//                            }
//                        }
//                }
//
//            }
//
//            override fun onChildViewAttachedToWindow(p0: View) {
//                L.i("SS进入：$p0")
//                val jzvd = p0.findViewById<JzvdStd>(R.id.videoPlayer)
//                jzvd?.startButton?.performClick()
//            }
//
//        })
    }

    fun setVideoInfo(t: VideoOtherInfo?) {
        mAdapter.data[mRcyCurrentPosition].videoOtherInfo = t
        mAdapter.notifyItemChanged(mRcyCurrentPosition)
    }

    fun setDatas(datas: BaseList<VideoClipInfo>?) {
        if (datas?.current_page!! > datas?.last_page!!) {
            smartRefreshLayout.finishLoadMoreWithNoMoreData()
            return
        }
        val data = datas.data
        smartRefreshLayout.finishLoadMore(true)
        if (mPlayPosition>9&&mAdapter.data.size==0){
            oldData?.addAll(data)
            mAdapter.setNewData(oldData)
            rcy.scrollToPosition(mRcyCurrentPosition)
            p.getVideoDetail(mAdapter.data[mRcyCurrentPosition].id)
        }else{
            mAdapter.autoPlay = false
            mAdapter.addData(data)
        }
//        if (mCurrentPage == 1) {
//            mAdapter.setNewData(data)
//            mRcyCurrentPosition = mPlayPosition//记录第一次滚动时 rcy选中的位置
//            if (mPlayPosition != -1) {
//                rcy.scrollToPosition(mPlayPosition)
//                p.getVideoDetail(mAdapter.data[mPlayPosition].id)
//            }
//        } else {
//            mAdapter.addData(data)
//        }

        mCurrentPage = datas.current_page
        dismissLoading()
    }

    override fun onResume() {
        super.onResume()
        JzvdStd.goOnPlayOnResume()
    }

    override fun onBackPressed() {
        if (Jzvd.backPress()) {
            return
        }
        super.onBackPressed()
    }

    override fun onPause() {
        super.onPause()
        JzvdStd.goOnPlayOnPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        JzvdStd.releaseAllVideos()
    }
}